home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / FTP.C < prev    next >
C/C++ Source or Header  |  1988-11-29  |  3KB  |  153 lines

  1. /* Stuff common to both the FTP server and client */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "netuser.h"
  6. #include "timer.h"
  7. #include "tcp.h"
  8. #include "iface.h"
  9. #include "ax25.h"
  10. #include "lapb.h"
  11. #include "ftp.h"
  12. #include "session.h"
  13.  
  14. /* FTP Data channel Receive upcall handler */
  15. void
  16. ftpdr(tcb,cnt)
  17. struct tcb *tcb;
  18. int16 cnt;
  19. {
  20.     register struct ftp *ftp;
  21.     struct mbuf *bp;
  22.     char c;
  23.  
  24.     ftp = (struct ftp *)tcb->user;
  25.     if(ftp->state != RECEIVING_STATE){
  26.         close_tcp(tcb);
  27.         return;
  28.     }
  29.     /* This will likely also generate an ACK with window rotation */
  30.     recv_tcp(tcb,&bp,cnt);
  31.  
  32. #if (UNIX || MAC || AMIGA || ATARI_ST)
  33.     if(ftp->type == ASCII_TYPE){
  34.         while(pullup(&bp,&c,1) == 1){
  35.             if(c != '\r')
  36.                 putc(c,ftp->fp);
  37.         }
  38.         return;
  39.     }
  40. #endif
  41.     while(bp != NULLBUF){
  42.         if(bp->cnt != 0)
  43.             fwrite(bp->data,1,(unsigned)bp->cnt,ftp->fp);
  44.         bp = free_mbuf(bp);
  45.     }
  46. }
  47. /* FTP Data channel Transmit upcall handler */
  48. void
  49. ftpdt(tcb,cnt)
  50. struct tcb *tcb;
  51. int16 cnt;
  52. {
  53.     struct ftp *ftp;
  54.     struct mbuf *bp;
  55.     register char *cp;
  56.     register int c;
  57.     int eof_flag;
  58.  
  59.     ftp = (struct ftp *)tcb->user;
  60.     if(ftp->state != SENDING_STATE){
  61.         close_tcp(tcb);
  62.         return;
  63.     }
  64.     if((bp = alloc_mbuf(cnt)) == NULLBUF){
  65.         /* Hard to know what to do here */
  66.         return;
  67.     }
  68.     eof_flag = 0;
  69.     if(ftp->type == IMAGE_TYPE){
  70.         bp->cnt = fread(bp->data,1,cnt,ftp->fp);
  71.         if(bp->cnt != cnt)
  72.             eof_flag = 1;
  73.     } else {
  74.         cp = bp->data;
  75.         while(cnt > 1){
  76.             if((c = getc(ftp->fp)) == EOF){
  77.                 eof_flag=1;
  78.                 break;
  79.             }
  80. #if (CPM || MSDOS) /*(defined(CPM) || defined(MSDOS)) DG2KK */
  81.             /* ^Z is CP/M's text EOF marker, and it is sometimes used
  82.              * by MS-DOS editors too
  83.              */
  84.             if(c == CTLZ){
  85.                 eof_flag = 1;
  86.                 break;
  87.             }
  88. #endif
  89. #if (UNIX || MAC || AMIGA || ATARI_ST)/*(defined(UNIX) || defined(MAC) || defined(AMIGA)) DG2KK */
  90.             if(c == '\n'){
  91.                 *cp++ = '\r';
  92.                 bp->cnt++;
  93.                 cnt--;
  94.             }
  95. #endif
  96.             *cp++ = c;
  97.             bp->cnt++;
  98.             cnt--;
  99.         }
  100.     }
  101.     if(bp->cnt != 0)
  102.         send_tcp(tcb,bp);
  103.     else
  104.         free_p(bp);
  105.  
  106.     if(eof_flag){    /* EOF seen */
  107.         fclose(ftp->fp);
  108.         ftp->fp = NULLFILE;
  109.         close_tcp(tcb);
  110.     }
  111. }
  112. /* Allocate an FTP control block */
  113. struct ftp *
  114. ftp_create(bufsize)
  115. unsigned bufsize;
  116. {
  117.     void ftp_delete();
  118.     register struct ftp *ftp;
  119.  
  120.     if((ftp = (struct ftp *)calloc(1,sizeof (struct ftp))) == NULLFTP)
  121.         return NULLFTP;
  122.     if(bufsize != 0 && (ftp->buf = malloc(bufsize)) == NULLCHAR){
  123.         ftp_delete(ftp);
  124.         return NULLFTP;
  125.     }
  126.     ftp->state = COMMAND_STATE;
  127.     ftp->type = ASCII_TYPE;    /* Default transfer type */
  128.     return ftp;
  129.  
  130. }
  131. /* Free resources, delete control block */
  132. void
  133. ftp_delete(ftp)
  134. register struct ftp *ftp;
  135. {
  136.     if(ftp->fp != NULLFILE && ftp->fp != stdout)
  137.         fclose(ftp->fp);
  138.     if(ftp->data != NULLTCB)
  139.         del_tcp(ftp->data);
  140.     if(ftp->username != NULLCHAR)
  141.         free(ftp->username);
  142.     if(ftp->path != NULLCHAR)
  143.         free(ftp->path);
  144.     if(ftp->buf != NULLCHAR)
  145.         free(ftp->buf);
  146.     if(ftp->cd != NULLCHAR)
  147.         free(ftp->cd);
  148.     if(ftp->session != NULLSESSION)
  149.         freesession(ftp->session);
  150.     free((char *)ftp);
  151. }
  152.  
  153.